package com.ricky;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by bgt on 2018/5/1.
 * 配置文件默认的路径是在classpath下根目录或者classpath:config/目录下
 * file:/ 或 file:config/
 * 可以通过--spring.config.name来指定配置文件的名称  不用写后缀名
 * --spring.config.name=app
 * 也可以通过--spring.config.location来指定路径
 *  --spring.config.location=classpath:config/app.properties,file:d:/tmp/tomcat.properties
 *
 */
//@SpringBootApplication
public class SpringBootStart01 {

    @Value("${name}")
    private String name;
    //：9090是给默认值
    @Value("${tomcat.port:9090}")
    private String tomcatport;

    @Autowired
    private Environment env;
    @Bean
    public Runnable createRun(){
        return ()->{
            System.out.println("我获取到name:"+name);
            System.out.println("我获取到name2:"+env.getProperty("name2"));
            System.out.println("我获取到appName:"+env.getProperty("app.name"));
            System.out.println("我执行了呢");
        };
    }

    public static void main(String[] args) {
//        ConfigurableApplicationContext context = SpringApplication.run(SpringBootStart01.class, args);
//        context.getBean(Runnable.class).run();

//        context.close();
        SpringApplication app=new SpringApplication();
        Set<Class<?>> set=new HashSet<>();
        set.add(SpringBootStart01.class);
        app.addPrimarySources(set);
        ConfigurableApplicationContext run = app.run(args);
        run.getBean(Runnable.class).run();

        //yml的操作不了？
        System.out.println( run.getEnvironment().getProperty("rickyip"));
        System.out.println( run.getEnvironment().getProperty("rickyip2"));
        System.out.println( run.getEnvironment().getProperty("user3"));

    }
}
